Ext.onReady(function() {
	
	//显示提示信息
	Ext.QuickTips.init();
	
	//统一设置提示风格，为side
	Ext.form.Field.prototype.msgTarget='side';	
	
	//片路径：一张1×1的透明图片，Ext使用它来和CSS一起创建内嵌的图标。 
	Ext.BLANK_IMAGE_URL = '../extjs/resources/images/default/s.gif';
	
	//checkbox选择模型
	var selModel = new Ext.grid.CheckboxSelectionModel();
	
	//数据源
 	var approvedTroubleTicketStore = new Ext.data.JsonStore({
 		url: '../personnelRequirement/approvedList.action',
		fields: ['_id', 'stationName', 'department', 'recruitNum', 'recruitReason', 'positionTime','createTime','happenTime','status'],
		root: 'data', 
		totalProperty: 'total' 
		});
	approvedTroubleTicketStore.load({
		params: {
			start: 0,
			limit: 10
		}
	});	
	
	//checkbox选择模型
	var selModel = new Ext.grid.CheckboxSelectionModel();
	
	//列模型
	var colModel = new Ext.grid.ColumnModel([
		new Ext.grid.RowNumberer({header: '序号', width: 35}), //加入序号
		selModel, //加入checkbox选择模型
		{header: '岗位名称', width: 100, dataIndex: 'stationName', sortable: true},
		{header: '部门名称', width: 100, dataIndex: 'department', sortable: true},
		{header: '招聘人数', width: 100, dataIndex: 'recruitNum', sortable: true},
		{header: '招聘原因', width: 100, dataIndex: 'recruitReason', sortable: true},
		{header: '到岗时间', width: 100, dataIndex: 'positionTime', sortable: true},
		{header: '发生时间', width: 100, dataIndex: 'happenTime', sortable: true},
		{header: '创建时间', width: 100, dataIndex: 'createTime', sortable: true},
		{header: '状态', width: 100, dataIndex: 'status', sortable: true}
	]);

	var approvedTroubleTicketGrid = new Ext.grid.GridPanel({
		title: '已审请假单列表',
		height: 450,
		autoWidth: true, 
		viewConfig: new Ext.grid.GridView({
			autoFill: true  
		}),
		stripeRows: true, 
		sm: selModel, 
		frame: true, 
		applyTo: 'approvedIndex_div',
		store: approvedTroubleTicketStore,
		cm: colModel, //列模型
		tbar: new Ext.Toolbar([
			{text: '审批历史', icon: '../images/viewHistory.gif', handler: viewHistory}
		]),
		bbar: new Ext.PagingToolbar({
			store: approvedTroubleTicketStore, 
			pageSize: 10,
			displayInfo: true,
			plugins: new Ext.ux.ProgressBarPager()
		})
	})
	
 	var historyStore = new Ext.data.JsonStore({
			url: '../approve/history.action',
			fields: ['desc', 'approveTime', 'approver']
		});
	
	var historyColModel = new Ext.grid.ColumnModel([
		new Ext.grid.RowNumberer({header: '序号', width: 35}), //加入序号
		{header: '描述', width: 100, dataIndex: 'desc', sortable: true},
		{header: '审批时间', width: 100, dataIndex: 'approveTime', sortable: true},
		{header: '审批人', width: 100, dataIndex: 'approver', sortable: true}
	]);
	
	var historyGrid = new Ext.grid.GridPanel({
		height: 200,
		autoWidth: true, 
		viewConfig: new Ext.grid.GridView({
			autoFill: true  
		}),
		stripeRows: true, 
		frame: true, 
		store: historyStore,
		cm: historyColModel
	});
			
	var historyWin = new Ext.Window({
		width: 400,
		height: 260,
		items: [historyGrid],
		modal: true, //模态
		resizable: false, //禁制调整窗口大小
		closeAction: 'hide' //不销毁form对象，只是隐藏
	})
	
	function viewHistory() {
		var records = approvedTroubleTicketGrid.getSelectionModel().getSelections();
		if (records.length == 0) {
			Ext.Msg.alert('提示', '请选择请假单！');	
		}else if (records.length > 1) {
			Ext.Msg.alert('提示', '一次只能选择一个请假单！');
		}else {
			historyWin.setTitle("审批信息列表");
			historyWin.show();
			historyStore.load({
				params:{"approveInfo.troubleTicket.id":records[0].get('id')}
			});	
		}
	}
})